// 用户点击登录的时候发起 登录请求，请求成功以后 跳转到 首页
let isUsername = false;
let isPassword = false;
let ipts = document.querySelectorAll(".input input");
let btns = document.querySelector("#btn");
ipts[0].onblur = function() {
	let reg = /^[a-z]\w{5,17}$/i;
	if (reg.test(this.value)) {
		isUsername = true;
	} else {
		isUsername = false;
	}
};
ipts[1].onblur = function() {
	let reg = /^\S{6,16}$/;
	if (reg.test(this.value)) {
		isPassword = true;
	} else {
		isPassword = false;
	}
};
btns.onclick = function() {
	if(ipts[0].value==""&&ipts[1].value==""){
		alert("账户密码不能为空")
	}
	if (isUsername && isPassword) {
		// 发起登录请求
		let username = ipts[0].value;
		let password = ipts[1].value;
		axios.get(loginAPI, {
			params: {
				username,
				password
			}
		}).then(function(res) {
			let obj = res.data;
			console.log(res)
			console.log(obj);
			// console.log(obj.data.id)
			
			alert(obj.msg);
			if (obj.code == 1) {
				localStorage.setItem("uid", obj.data.id);
				localStorage.setItem("username", obj.data.username);
				localStorage.setItem("token", obj.data.token);
				location.href = "index.html";
			}
			
		});
	}
}
